const initialState = {
  detail: {},
  comment: {},
  reply: {}
}
export default function acticleReducer(state = initialState, action) {
  switch (action.type) {
    // 获取文章详情
    case "GET_ACTICLEDETAIL":
      return {
        ...state,
        detail: action.payload
      }
    case "UPDATE_ARTICLE":
      const { name, value } = action.payload
      return {
        ...state,
        detail: {
          ...state.detail,
          [name]: value
        }
      }
    // 保存文章评论列表
    case "GET_COMMENT":
      return {
        ...state,
        comment: action.payload
      }
    case "GET_REPLY":
      return {
        ...state,
        reply: action.payload
      }
    // 更新评论信息
    case "UPDATE_COMMENT":
      const { id, isLiking } = action.payload
      return {
        ...state,
        comment: {
          ...state.comment,
          results: state.comment.results.map((item) => {
            if (item.com_id === id) {
              return {
                ...item,
                // 点赞状态值
                is_liking: isLiking,
                // 修改数量
                like_count: isLiking ? item.like_count + 1 : item.like_count - 1
              }
            }
            return item
          })
        }
      }
    // 更新回复信息
    case "UPDATE_REPLY":
      const { rid, liking } = action.payload
      return {
        ...state,
        reply: {
          ...state.reply,
          results: state.reply.results.map((item) => {
            if (item.com_id === rid) {
              return {
                ...item,
                is_liking: liking,
                like_count: liking ? item.like_count + 1 : item.like_count - 1
              }
            }
            return item
          })
        }
      }
    case "UPDATE_FOLLOW":
      const { commId, comName, comValue } = action.payload
      return {
        ...state,
        comment: {
          ...state.comment,
          results: state.comment.results.map((item) => {
            console.log(item.aut_id)
            console.log(commId)
            if (item.aut_id === commId) {
              return {
                ...item,
                [comName]: comValue
              }
            }
            return item
          })
        }
      }
    default:
      return state
  }
}
